package com.example.hdwx.myapplication.HomeActivity;

import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hdwx.myapplication.HomeActivity.cooklist_fragment.Cooklist_Fragment;
import com.example.hdwx.myapplication.HomeActivity.daydaycook_fragment.Daydaycook_Fragment;
import com.example.hdwx.myapplication.HomeActivity.found_fragment.Found_Fragment;
import com.example.hdwx.myapplication.HomeActivity.goodchoice_fragment.Goodchoice_Fragment;
import com.example.hdwx.myapplication.HomeActivity.myself_fragment.Myself_Fragment;
import com.example.hdwx.myapplication.R;

public class HomeActivity extends AppCompatActivity {
    ImageView img1,img2,img3,img4,img5;
    Daydaycook_Fragment daydaycook_fragment;
    Cooklist_Fragment cooklist_fragment;
    Found_Fragment found_fragment;
    Goodchoice_Fragment goodchoice_fragment;
    Myself_Fragment myself_fragment;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        init();
    }

    private void init() {
         //给变量赋值；
        find_id();
        //设置程序运行时首先显示的
        setFragmentManager(daydaycook_fragment);
        img1.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_selected_active));
        //点击图片切换碎片
        img1_onclicked();
        img2_onclicked();
        img3_onclicked();
        img4_onclicked();
        img5_onclicked();
    }

    private void find_id() {
        daydaycook_fragment=new Daydaycook_Fragment();
        cooklist_fragment=new Cooklist_Fragment();
        found_fragment=new Found_Fragment();
        goodchoice_fragment=new Goodchoice_Fragment();
        myself_fragment=new Myself_Fragment();
        img1= (ImageView) findViewById(R.id.daydaycook_img);
        img2= (ImageView) findViewById(R.id.cooklist_img);
        img3= (ImageView) findViewById(R.id.goodchoice_img);
        img4= (ImageView) findViewById(R.id.found_img);
        img5= (ImageView) findViewById(R.id.myself_img);
        textView= (TextView) findViewById(R.id.kongbai_homeActivity);
    }

    //封装点击切换碎片和图片取消选中
    public void setFragmentManager(Fragment fragment){
        textView.setVisibility(View.VISIBLE);
        //取消选中图片
        img1.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_selected_default));
        img2.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_recipe_default));
        img3.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_shop_default));
        img4.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_find_default));
        img5.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_my_default));
        //切换碎片
        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.replace(R.id.Fragment_homeActivity,fragment);
        transaction.commit();

    }

    private void img5_onclicked() {
        img5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragmentManager(myself_fragment);

                textView.setVisibility(View.GONE);
                img5.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_my_activepng));

            }
        });
    }

    private void img4_onclicked() {
        img4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragmentManager(found_fragment);
                img4.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_find_active));
            }
        });
    }

    private void img3_onclicked() {
        img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragmentManager(goodchoice_fragment);
                img3.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_shop_active));
            }
        });
    }

    private void img2_onclicked() {
        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragmentManager(cooklist_fragment);
                img2.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_recipe_active));
            }
        });
    }

    private void img1_onclicked() {
        img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragmentManager(daydaycook_fragment);
                img1.setImageDrawable(getResources().getDrawable(R.drawable.home_btn_selected_active));
            }
        });
    }
}
